<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Admin\Controller;
use App\Http\Requests\Api\ActivityEditRequest;
use App\Http\Requests\Api\ActivityRequest;
use App\Http\Requests\Api\CommentRequest;
use App\Models\Activity;
use App\Models\ActivityComment;
use App\Models\ActivityProcess;
use App\Models\Order;
use App\Models\UserFavorite;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class ActivityController extends Controller
{



    //活动列表
    public function getUserActivityList(Request $request){


        $title = $request->title;
        $user=Auth::guard('api')->user();

        $list = Activity::with(['order' => function ($query) use ($user) {
            $query->where('pay_status',1)->where('order_status','<',4);
        },])->when($user, function ($query, $user) {
            return $query->with(['favorite'=>function ($query) use ($user)  {
                    $query->where('user_id', $user['id']);
            }]);
        })->when($title,function($query) use ($title){
                return $query->where('title', 'like', '%'.$title.'%');

        })->where('is_del',0)->select(['id','title','start_time','end_time','join_time','status','cover_img'])
            ->orderBy('id','desc')
            ->paginate($request->pageSize?:15);

        if($list[0]['order']){
            foreach ($list as $k=>$v){

                foreach ($v['order'] as $kk=>$vv){
                    $list[$k]['order_person']+=$vv['person'];
                }

                unset($list[$k]['order']);
            }
        }



        return $this->success($list);
    }


    //活动详情
    public function getUserActivityDetail(Request $request){

        $id = $request->id;

        if(!$id){
           return $this->failed("id必传");
        }else{
            $user=Auth::guard('api')->user();
           $info = Activity::when($user, function ($query, $user) {
               return $query->with(['order' => function ($query) use ($user) {
                   $query->where('user_id', $user['id'])->where('pay_status',1);
               },'favorite'=>function ($query) use ($user)  {
                   $query->where('user_id', $user['id']);
               }]);
           })->with('process')->find($id);

            $order_count = Order::with('order_user')->where('activity_id',$id)->where('pay_status',1)->where('order_status','<',4)->get();

            if($order_count){
                foreach ($order_count as $k=>$v){
                    $info->already_person += $v['person'];
                }
            }


           return $this->success($info);
        }

    }




    //发表评论
    public function addComment(CommentRequest $request){

        $user=Auth::guard('api')->user();

        ActivityComment::create(array_merge($request->all(),['user_id'=>$user['id']]));

        return $this->success('评价成功');

    }

    //根据活动获取评论列表
    public function getCommentList(Request $request){

        $activity_id = $request->activity_id;
        $activity = Activity::find($activity_id);

        if(!$activity){

            return $this->failed("活动不存在");

        }

        $comments = ActivityComment::with('user')->where('activity_id',$activity_id)
            ->orderBy('id','desc')
            ->paginate($request->pageSize?:15);

        return $this->success($comments);
    }



    //添加收藏
    public function addFavorite(Request $request)
    {
        $activity_id = $request->activity_id;
        $activity = Activity::find($activity_id);

        if(!$activity){

            return $this->failed("活动不存在");

        }

        $user=Auth::guard('api')->user();

        $fa = UserFavorite::where('user_id',$user['id'])->where('activity_id',$activity_id)->first();

        if($fa)

            return $this->failed('已收藏过');

        $res = UserFavorite::create(['activity_id'=>$activity_id,'user_id'=>$user['id']]);

        if($res)
            return $this->success("收藏成功");

    }

    //获取收藏列表
    public function getFavoriteList(Request $request){

        $user=Auth::guard('api')->user();

        $list = UserFavorite::with('activity:id,title,start_time')->where('user_id',$user['id'])->get();

        return $this->success($list);

    }


    //取消收藏
    public function deleteFavorite(Request $request)
    {
        $activity_id = $request->activity_id;
        $activity = Activity::find($activity_id);

        if(!$activity){

            return $this->failed("活动不存在");

        }

        $user=Auth::guard('api')->user();

        $fa = UserFavorite::where('user_id',$user['id'])->where('activity_id',$activity_id)->delete();

        if($fa)
            return $this->failed('取消成功');



    }


}
